#include <stdio.h>

int do_BS(int nums[], int target, int l, int r)
{
    int mid = l + (r-l)/2;
    if (r < l)
        return -1;
    if (target < nums[mid])
        return do_BS(nums, target, l, mid-1);
    else if (target > nums[mid])
        return do_BS(nums, target, mid+1, r);
    return mid;
}

int binarySearch(int nums[], int size, int target)
{
    return do_BS(nums, target, 0, size-1);
}

int main(void)
{
    printf("二分查找\n\n");
    int nums[] = {1,4};
    int target = 4;
    int idx = binarySearch(nums, sizeof(nums)/sizeof(int), target);
    
    printf("target %d, index %d\n", target, idx);
    return 0;
}